﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;


namespace YesBlog.Domain
{
    public interface IRepository<TEntity> where TEntity : class
    {
        /// <summary>
        /// 实体集合
        /// </summary>
        IQueryable<TEntity> Table { get; }

        /// <summary>
        /// 实体集合AsNoTracking
        /// </summary>
        IQueryable<TEntity> TableNoTracking { get; }

        /// <summary>
        /// 按条件查询实体
        /// </summary>
        /// <param name="where"></param>
        /// <param name="isTracking"></param>
        /// <returns></returns>
        IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> where, bool isTracking = false);

        /// <summary>
        /// 统计总数
        /// </summary>
        /// <returns></returns>
        int Count();

        /// <summary>
        /// 按条件统计
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        int Count(Expression<Func<TEntity, bool>> where);

        /// <summary>
        /// 根据主键得到一个实体
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        TEntity Get(object key);

        /// <summary>
        /// 根据组合主键得到一个实体
        /// 顺序需要根据实体类主键保持一致
        /// Column(Order = 顺序)
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        TEntity Get(params object[] keys);


        /// <summary>
        /// 按条件查询单个实体
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        TEntity Get(Expression<Func<TEntity, bool>> where);

        /// <summary>
        /// 新增一个实体
        /// </summary>
        /// <param name="entity"></param>
        void Insert(TEntity entity);

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entites"></param>
        void Insert(IEnumerable<TEntity> entites);

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity"></param>
        void Update(TEntity entity);

        /// <summary>
        /// 根据条件批量更新
        /// </summary>
        /// <param name="where"></param>
        /// <param name="func"></param>
        void Update(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TEntity>> func);
        

        /// <summary>
        /// 更新多个实体
        /// </summary>
        /// <param name="entities"></param>
        void Update(IEnumerable<TEntity> entities);

        /// <summary>
        /// 删除实体数据
        /// </summary>
        /// <param name="entity"></param>
        void Delete(TEntity entity);

        /// <summary>
        /// 按条件删除
        /// </summary>
        /// <param name="where"></param>
        void Delete(Expression<Func<TEntity, bool>> where);

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entities"></param>
        void Delete(IEnumerable<TEntity> entities);


    }


}
